package com.nj9you.pay.activity;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;

import com.nj9you.pay.utils.Constants;
import com.nj9you.pay.utils.Utils;
import com.tencent.mm.sdk.constants.ConstantsAPI;
import com.tencent.mm.sdk.modelbase.BaseReq;
import com.tencent.mm.sdk.modelbase.BaseResp;
import com.tencent.mm.sdk.openapi.IWXAPI;
import com.tencent.mm.sdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.sdk.openapi.WXAPIFactory;

@SuppressLint("NewApi")
public class WXPayEntryActivity extends Activity implements IWXAPIEventHandler {

    private static final String TAG = "njsdk";
    private IWXAPI api;
    private Context paramContext;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        api = WXAPIFactory.createWXAPI(this, Constants.APP_ID);
        api.handleIntent(getIntent(), this);
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        setIntent(intent);
        api.handleIntent(intent, this);
    }

    @Override
    public void onReq(BaseReq req) {
    }

    @Override
    public void onResp(BaseResp resp) {
        Log.d(TAG, "onResp, errCode = " + resp.errCode + " , msg : " + resp.errStr);
        String desc = null;
        if (resp.getType() == ConstantsAPI.COMMAND_PAY_BY_WX) {
            if (resp.errCode == 0) {
                desc = Utils.getString(this, "pay_success");
            } else if (resp.errCode == -1) {
                desc = Utils.getString(this, "pay_failure");
            } else if (resp.errCode == -2) {
                desc = Utils.getString(this, "pay_cancel");
            } else {
                desc = Utils.getString(this, "pay_unknown");
            }
            sendPayResult(resp.errCode, desc);
        }
        finish();
    }

    private void sendPayResult(int errCode, String desc) {
        Utils.sendPayResult(this, errCode, desc);
    }
}